package com.jzl.swing.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jzl.swing.dao.UserDao;
import com.jzl.swing.entity.User;
import com.jzl.swing.rowmapper.impl.UserRowMapper;
import com.jzl.swing.util.JDBCTemplate;

/**
 * 用户管理实现类
 * @author zalej
 *
 */
public class UserDaoImpl implements UserDao{

	@Override
	public int add(User user) throws SQLException {
		String sql = "insert into users(user_name,user_pass,user_iden)values(?,?,?)";
		return JDBCTemplate.executeUpdate(sql, user.getUser_name(),user.getUser_pass(),user.getUser_iden());
	}

	@Override
	public int modify(User user) throws SQLException {
		String sql = "update users set user_pass=? where user_name=?";
		return JDBCTemplate.executeUpdate(sql, user.getUser_pass(), user.getUser_name());
	}

	@Override
	public int delete(User user) throws SQLException {
		String sql = "delete from users where user_name=?";
		
		return JDBCTemplate.executeUpdate(sql, user.getUser_name());
	}

	@Override
	public List<User> queryAll() throws SQLException {
		List<User> userList = new ArrayList<User>();
		String sql = "select * from users";
		List<Object> list = JDBCTemplate.executeQuery(sql, new UserRowMapper());
		for (Object object : list) {
			userList.add((User)object);
		}
		return userList;
	}

	@Override
	public User queryByName(String name) throws SQLException {
		String sql = "select * from users where user_name=?";
		List<?> list = JDBCTemplate.executeQuery(sql, new UserRowMapper(), name);
		return list.size()>0 ? (User)list.get(0):null;
	}
	
	
	
}
